import 'package:backendview/widget/custom_form_item.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';

//文本输入框
class CustomTextField extends StatefulWidget implements CustomFormItem<String> {
  late TextEditingController controller;

  bool? required = false;
  String label;
  double? labelMaxWidth;
  TextStyle? labelStyle;
  String? value;
  TextStyle? valueStyle;
  String? hintText;
  bool? hintTextShow;
  final GestureTapCallback? onTap;
  TextInputType? keyboardType;
  bool? readOnly;
  List<Widget>? rightAction;
  Function(String)? onChanged;
  Function(bool hasFocus)? onFocusChange;
  final VoidCallback? onEditingComplete;
  List<TextInputFormatter>? inputFormatters;
  bool? obscureText;
  int? maxLines;
  int? minLines;
  int? maxLength;
  bool? isDense;
  bool? isCollapsed;
  bool updateFocus;
  EdgeInsetsGeometry? contentPadding;

  TextAlign textAlign;

  CustomTextField(
    this.label, {
    super.key,
    this.required,
    this.labelMaxWidth = 200,
    this.labelStyle,
    this.value,
    this.valueStyle,
    this.keyboardType,
    this.rightAction,
    this.hintText,
    this.hintTextShow = true,
    this.readOnly,
    this.onChanged,
    this.textAlign = TextAlign.center,
    this.inputFormatters,
    this.onEditingComplete,
    this.obscureText,
    this.maxLines = 1, //若允许多行则传入null
    this.minLines = 1, //若允许多行则传入null
    this.maxLength,
    this.updateFocus = true,
    this.onTap,
    this.isDense,
    this.isCollapsed,
    this.contentPadding,
    this.onFocusChange,
  }) {
    controller =
        TextEditingController.fromValue(TextEditingValue(text: value ?? ""));
  }

  @override
  State<StatefulWidget> createState() {
    return _CustomTextField();
  }

  String getText() {
    return controller.text;
  }

  @override
  String getFullValue() {
    return controller.text;
  }

  @override
  String? getTypeValue() {
    return controller.text;
  }

  @override
  String getValue() {
    return controller.text;
  }
}

class _CustomTextField extends State<CustomTextField> {
  late TextEditingController controller;
  final FocusNode _focusNode = FocusNode();

  // _CustomTextField(){
  //  controller
  // }
  @override
  void initState() {
    print("initState");
    controller = widget.controller;
    controller.text = widget.value ?? "";
    super.initState();
    //添加监听
    _focusNode.addListener(() {
      if (!_focusNode.hasFocus) {
        print("----失去焦点了----");
        widget.onFocusChange?.call(false);
      } else {
        print("----获得焦点了----");
        widget.onFocusChange?.call(true);
      }
    });
  }

  @override
  void didUpdateWidget(covariant CustomTextField oldWidget) {
    super.didUpdateWidget(oldWidget);
    if (oldWidget.updateFocus != widget.updateFocus) {
      print("获取焦点");
      _focusNode.requestFocus();
    }
  }

  @override
  void dispose() {
    _focusNode.unfocus();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    widget.controller = controller;
    if (widget.value != controller.text) {
      controller.text = widget.value ?? "";
    }

    return Row(
      mainAxisSize: MainAxisSize.min,
      mainAxisAlignment: MainAxisAlignment.start,
      children: [
        widget.required == true
            ? const SizedBox(
                width: 10,
                child: Center(
                  child: Text(
                    "*",
                    style: TextStyle(
                      fontSize: 15,
                      color: Color(0xFFFF685C),
                    ),
                  ),
                ),
              )
            : Container(),
        Container(
          constraints: widget.labelMaxWidth == null
              ? null
              : BoxConstraints(maxWidth: widget.labelMaxWidth!),
          child: Text(
            widget.label,
            style: widget.labelStyle ??
                const TextStyle(
                    color: Colors.black,
                    fontSize: 15,
                    fontWeight: FontWeight.bold),
          ),
        ),
        const SizedBox(
          width: 8,
        ),
        SizedBox(
            width: 100,
            child: TextField(
              controller: controller,
              onTap: widget.onTap,
              onEditingComplete: widget.onEditingComplete,
              style: widget.valueStyle ??
                  const TextStyle(color: Color(0xff333333), fontSize: 15),
              textAlign: widget.textAlign,
              readOnly: widget.readOnly ?? false,
              keyboardType: widget.keyboardType ?? TextInputType.text,
              inputFormatters: widget.inputFormatters,
              obscureText: widget.obscureText ?? false,
              minLines: widget.minLines,
              maxLines: widget.maxLines,
              maxLength: widget.maxLength,
              focusNode: _focusNode,
              decoration: InputDecoration(
                  isDense: widget.isDense ?? false,
                  isCollapsed: widget.isCollapsed ?? false,
                  counterText: "",
                  contentPadding: widget.contentPadding,
                  hintText: widget.hintTextShow == true
                      ? widget.hintText ?? "请输入${widget.label}"
                      : "",
                  border: InputBorder.none,
                  hintStyle: const TextStyle(color: Colors.grey, fontSize: 15)),
              onChanged: (value) {
                widget.value = value;
                widget.onChanged?.call(controller.text);
              },
            )),
        widget.rightAction?.isNotEmpty == true
            ? Row(
                children: List.generate(widget.rightAction?.length ?? 0,
                    (index) => widget.rightAction![index]),
              )
            : Container(),
      ],
    );
  }
}
